<template>
    <div v-if="ishead">
        <template v-if="itemdata.lCanInput === '0'">
            <van-field size="large" type="number" readonly clickable :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true"  />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="showKeyboardInt = false" theme="custom" close-button-text="完成" />
        </template>
        <template v-else-if="itemdata.lCanInput === '1' && itemdata.lMustInput === '1'">
            <van-field size="large" type="number" readonly clickable required :id="itemdata.sField" placeholder="请输入" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true"  />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="onblur(itemdata)" @input="checkData(itemdata)" theme="custom" close-button-text="完成" />
        </template>
        <template v-else>
            <van-field size="large" type="number" readonly clickable :id="itemdata.sField" placeholder="请输入" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true"  />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="showKeyboardInt = false"  theme="custom" close-button-text="完成" />
        </template>
    </div>
    <div v-else>
        <template v-if="itemdata.lCanInput === '0'">
            <van-field size="large" type="number" readonly clickable :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true" />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="showKeyboardInt = false" theme="custom" close-button-text="完成" />
        </template>
        <template v-else-if="itemdata.lCanInput === '1'  && itemdata.lMustInput === '1'">
            <van-field size="large" type="number" readonly clickable required placeholder="请输入" :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true" />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="onblur(itemdata)" @input="checkData(itemdata)" theme="custom" close-button-text="完成" />
        </template>
        <template v-else>
            <van-field size="large" type="number" readonly clickable placeholder="请输入" :id="index + '_' + itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardInt = true" />
            <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardInt" @close="showKeyboardInt = false" :show="showKeyboardInt"  @blur="showKeyboardInt = false" theme="custom" close-button-text="完成" />
        </template>
    </div>
</template>
<script>
export default {
  props: {
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  data() {
    return {
      showKeyboardInt: false,
      value: ''
    };
  },
  methods: {
    onblur(datamodel) {
      this.showKeyboardInt = false
      this.checkData (datamodel)
    },
    checkData (datamodel) {
      //console.log('checkData')
      let projectData = require('@/project.json')
      if (projectData.project && projectData.project == 'dinuo/') {
        //console.log('datamodel', datamodel)
        if (datamodel && datamodel.sField == 'quantity') {
          let value = datamodel.value
          if (value && value.indexOf('.') != -1) {
            datamodel.value = value.substring(0, value.indexOf('.'))
            Toast('数量不能有小数')
            return
          }
        }
        if (datamodel && datamodel.sField == 'price2') {
            let value = datamodel.value

            if (value && value.indexOf('.') != -1) {
              let v = value.substring(value.indexOf('.'))
              if (v.length > 3) {
                datamodel.value = value.substring(0, value.indexOf('.') + 3)
                Toast('小数点后面只能有两位')
                return
              }
            }

            if (value && value.startsWith('-')) {
              Toast('结算价不能是负数')
              return
            }
            for (let j = 0; j < this.jsondata.bodydata[this.indexName].length; j++) {
              for (let k = 0; k < this.jsondata.bodydata[this.indexName][j].length; k++) {
                //console.log('this.jsondata.bodydata[this.indexName][j]', this.jsondata.bodydata[this.indexName][j])
                //console.log('this.jsondata.bodydata[this.indexName][j][k]', this.jsondata.bodydata[this.indexName][j][k])

                if (j == this.index) {
                  let item = this.jsondata.bodydata[this.indexName][j][k]
                  //console.log('item-->', item)
                  if (item.sField === 'type') {
                    let typeValue = item.value
                    if (!typeValue) {
                      datamodel.value = ''
                      datamodel.valuehidden = ''
                      Toast('请先选择报量类型')
                      return
                    }
                    if (typeValue == '赠品' || typeValue == '礼品及物料') {
                      datamodel.value = 0
                      datamodel.valuehidden = 0
                      break
                    } else {
                      // 药品
                      let price2Value = datamodel.value
                      if (price2Value && (price2Value == '0' || price2Value == '0.0' || price2Value == '0.00')) {
                        datamodel.value = ''
                        datamodel.valuehidden = ''
                        return
                      }
                    }
                  }
                }
              }
            }
        }
      }

      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    }
  }
}
</script>
